<?php
  // avatars state update/request
  require_once dirname(__FILE__)."/../purin2/core/core.php";
  require_once dirname(__FILE__)."/../purin2/user/session.php";
  require_once dirname(__FILE__)."/../purin2/xtr/xtr.php";
  
  puXtrBegin("Update state of player's avatar, receive info about other's avatars");
  puXtrInputs(
    'i_x',         'integer',  'location on map where avatar is',
    'i_y',         'integer',  'location on map where avatar is',
    'v_action',    'string',   'walk, sit, stand, attack, die, ...',
    'v_animation', 'string',   'left, right, up, down, ...',
    'j_head',      'integer',  'head',
    'j_hair',      'integer',  'hairstyle',
    'i_red',       'integer',  'hairstyle color - red part',
    'i_green',     'integer',  'hairstyle color - green part',
    'i_blue',      'ineger',   'hairstyle color - blue part',
    'j_chest',     'integer',  'chest',
    'j_leg',       'integer',  'leg',
    'j_feet',      'integer',  'feet',
    'j_hands',     'integer',  'hands',
    'v_race',      'string',   'male/female',
    'v_chat',      'string',   'last chat message'
    );
  puXtrOutputs(
    'i_asta',      'integer',  'serial',
    'j_user',      'integer',  'avatar\'s owner',
    'j_tmap',      'integer',  'map where avatar is',
    'i_x',         'integer',  'location on map where avatar is',
    'i_y',         'integer',  'location on map where avatar is',
    'd_update',    'datetime', 'time of last update',
    'v_action',    'string',   'walk, sit, stand, attack, die, ...',
    'v_animation', 'string',   'left, right, up, down, ...',
    'j_head',      'integer',  'head',
    'j_hair',      'integer',  'hairstyle',
    'i_red',       'integer',  'hairstyle color - red part',
    'i_green',     'integer',  'hairstyle color - green part',
    'i_blue',      'ineger',   'hairstyle color - blue part',
    'j_chest',     'integer',  'chest',
    'j_leg',       'integer',  'leg',
    'j_feet',      'integer',  'feet',
    'j_hands',     'integer',  'hands',
    'v_race',      'string',   'male/female',
    'v_chat',      'string',   'last chat message',
    'i_chat',      'integer',  'chat unix timestamp',
    'i_server',    'integer',  'server unix timestamp'
  );
  
  // get input parameters
  $j_user = $pu_user_serial;
  if ($j_user <= 0)
    puXtrDie(1,'You must be logged in to update/read players position!');
  // send me map where I am, or map 1 if im first time on server
  $j_tmap = puSelect("select j_tmap from asta where j_user=$j_user");
  puXtrComment("user $j_user is on map $j_tmap");
  if ($j_tmap <= 0) {
    $j_tmap = 18;  // fallback map is hurnscald
  }
  //
  $i_x = puInt('i_x',-1);
  if ($i_x >= 0) {
    $i_y = puInt('i_y',-1);
    if ( ($i_x < 0) || ($i_y < 0) )
      puXtrDie(2,'Undefined position');
    // TODO: check possible map coordinates, colision cells, etc...
    $d_update = 'NOW()';
    $v_action = puStr('v_action','stand',array('walk','sit','stand','die','attack'));
    $v_animation = puStr('v_animation','down',array('left','right','up','down'));
    $j_head = puInt('j_head');
    $j_hair = puInt('j_hair');
    $i_red = puInt('i_red');
    $i_green = puInt('i_green');
    $i_blue = puInt('i_blue');
    $j_chest = puInt('j_chest');
    $j_leg = puInt('j_leg');
    $j_feet = puInt('j_feet');
    $j_hands = puInt('j_hands');
    $v_race = puStr('v_race','male',array('male','female'));
    // chat
    $v_chat = puStr('v_chat','');
    $chat_update = '';
    if (!empty($v_chat))
      $chat_update = ", v_chat='$v_chat', i_chat=UNIX_TIMESTAMP() ";
    // insert/update player values
    $sql = "insert into asta values (0, $j_user, $j_tmap, $i_x, $i_y, $d_update, '$v_action', '$v_animation', $j_head, $j_hair, $i_red, $i_green, $i_blue, $j_chest, $j_leg, $j_feet, $j_hands, '$v_race', '$v_chat', i_chat=UNIX_TIMESTAMP())";
    puXtrComment("SQL: $sql");
    $i_asta = puInsert($sql,true);
    puXtrComment("inserted i_asta = $i_asta");
    // if insert failed, we need only update
    if ($i_asta <= 0) {
      $sql = "update asta set i_x=$i_x, i_y=$i_y, d_update=NOW(), v_action='$v_action', v_animation='$v_animation', j_head=$j_head, j_hair=$j_hair, i_red=$i_red, i_green=$i_green, i_blue=$i_blue, j_chest=$j_chest, j_leg=$j_leg, j_feet=$j_feet, j_hands=$j_hands, v_race='$v_race' $chat_update where j_user=$j_user";
      $rows = puUpdate($sql,true);
      if ($rows < 0)
        puXtrDie(3,"Update failed: ".mysql_error());
      puXtrComment("rows updated $rows");
    }
  } else
    puXtrComment('Update ommited, i_x undefined, read-only mode');
  // select other values
  puXtrRowsSql("select *, UNIX_TIMESTAMP() as i_server from asta where j_tmap=$j_tmap",1);
  puXtrEnd(0,'Successfull');
?>